using System;
using System.Collections.Generic;
using System.Text;

namespace IGGCore
{
    class RatioCalculator
    {
        /// <summary>
        /// Set the interest ratio for a game
        /// </summary>
        public void SetRatio(Game game)
        {
            game.Ratio = 0;

            if (game.Status.Indicator.IsInProgress)
            {
                game.Ratio += 5000;
            }
            else if (game.Status.Indicator.IsInterrupted)
            {
                game.Ratio -= 5000;
            }
            else if (game.Status.Indicator.IsTerminated)
            {
                game.Ratio -= 1000;
            }
            game.Ratio += (game.Status.Inning * 10);

            // WIN PERCENTAGES OF BOTH TEAMS
            if (game.AwayTeam.WinPercent > 0.6)
            {
                UpdateRatio(game, 50, "Away team with over 0.6 win percent");
            }
            else if (game.AwayTeam.WinPercent < 0.36)
            {
                UpdateRatio(game, -50, "Away team with less than 0.36 win percent");
            }
            if (game.HomeTeam.WinPercent > 0.6)
            {
                UpdateRatio(game, 50, "Home team with over 0.6 win percent");
            }
            else if (game.HomeTeam.WinPercent < 0.36)
            {
                UpdateRatio(game, -50, "Home team with less than 0.36 win percent");
            }

            // really good teams
            if (game.AwayTeam.WinPercent > 0.7)
            {
                UpdateRatio(game, 20, "Away team with more than 0.7 win percent");
            }
            if (game.HomeTeam.WinPercent > 0.7)
            {
                UpdateRatio(game, 20, "Home team with more than 0.7 win percent");
            }

            // RUNNERS
            if (game.Status.RunnersOnBase[0] != null && game.Status.RunnersOnBase[1] != null && game.Status.RunnersOnBase[2] != null)
            {
                UpdateRatio(game, 100, "Bases are loaded!");
            }
            else if (game.Status.RunnersOnBase[1] != null && game.Status.RunnersOnBase[2] != null)
            {
                UpdateRatio(game, 60, "Runners on second and third");
            }
            else if (game.Status.RunnersOnBase[2] != null)
            {
                UpdateRatio(game, 40, "Runner on third base");
            }
            else if (game.Status.RunnersOnBase[1]!=null)
            {
                UpdateRatio(game, 20, "Runner on second");
            }

            // NEAR FIRST PLACE TEAM
            if (game.AwayTeam.GetNumericGamesBack() < 2)
            {
                UpdateRatio(game, 30, "Away team near or in first place");
            }
            if (game.HomeTeam.GetNumericGamesBack() < 2)
            {
                UpdateRatio(game, 30, "Home team near or in first place");
            }

            // WILD-CARD CHASE
            if (game.AwayTeam.GetNumericWildCardGamesBack() < 3)
            {
                UpdateRatio(game, 30, "Away team near wild-card");
            }
            if (game.HomeTeam.GetNumericWildCardGamesBack() < 3)
            {
                UpdateRatio(game, 30, "Home team near wild-card");
            }

            // LEAGUE/DIVISION
            // interleague
            if (game.AwayTeam.League != game.HomeTeam.League)
            {
                UpdateRatio(game, 100, "Interleague game");
            }
            else
            {
                // same division
                if (game.AwayTeam.Division == game.HomeTeam.Division)
                {
                    UpdateRatio(game, 75, "Same-Division game");

                    // chase of first place
                    if (game.AwayTeam.GetNumericGamesBack() == 0 
                        && game.HomeTeam.GetNumericGamesBack() < 3)
                    {
                        UpdateRatio(game, 150, "Close chase of first place");
                    }
                    else if (game.HomeTeam.GetNumericGamesBack() == 0 
                        && game.AwayTeam.GetNumericGamesBack() < 3)
                    {
                        UpdateRatio(game, 150, "Close chase of first place");
                     }
                }
            }

            // LATE IN THE GAME
            if (game.Status.Inning > 6 && game.Status.Indicator.IsInProgress)
            {
                UpdateRatio(game, 20, "Late in the game");

                // SCORE IS TIED
                if (game.Linescore.Diff == 0)
                {
                    UpdateRatio(game, 150, "Game is tied late in the game");
                 }
                // SCORE DIFF IS LOW
                else if (game.Linescore.Diff < 3)
                {
                    UpdateRatio(game, 100, "Game is close late in the game");
                }

                // POSSIBLE NO HITTERS
                if (game.Linescore.GetAwayLineScore().Hits == 0
                    || game.Linescore.GetHomeLineScore().Hits == 0)
                {
                    if (game.Status.Inning == 9)
                    {
                        UpdateRatio(game, 500, "9th inning possible no hitter");
                    }
                    else if (game.Status.Inning == 8)
                    {
                        UpdateRatio(game, 200, "8th inning possible no hitter");
                    }
                    else
                    {
                        UpdateRatio(game, 100, "7th inning possible no hitter");
                    }
                }

                // EXTRA-INNINGS
                if (game.Status.Inning > 18)
                {
                    UpdateRatio(game, 500, "19th inning and more");
                 }
                else if (game.Status.Inning > 15)
                {
                    UpdateRatio(game, 300, "16th inning and more");
                 }
                else if (game.Status.Inning > 13)
                {
                    UpdateRatio(game, 250, "14th inning and more");
                }
                else if (game.Status.Inning > 11)
                {
                    UpdateRatio(game, 150, "12th inning and more");
                }
                else if (game.Status.Inning > 9)
                {
                    UpdateRatio(game, 100, "10th inning and more");
                }

                // EARLY GAME
                if (game.Status.Inning < 3)
                {
                    UpdateRatio(game, -50, "Early in the game");
                }
            }

            // BLOWOUTS
            if (game.Linescore != null)
            {
                if (game.Linescore.Diff > 7)
                {
                    UpdateRatio(game, -150, "Big blowout");
                }
                else if (game.Linescore.Diff > 5)
                {
                    UpdateRatio(game, -100, "Blowout");
                }
                else if (game.Linescore.Diff > 4)
                {
                    UpdateRatio(game, -50, "Small blowout");
                }
            }

            // RED-SOX vs YANKEES
            CheckRivalry(game, "Red Sox", "Yankees", 50);

            // OTHER RIVALTIES
            CheckRivalry(game, "Dodgers", "Giants", 50);
            CheckRivalry(game, "Phillies", "Mets", 20);
            CheckRivalry(game, "Astros", "Cardinals", 20);
            CheckRivalry(game, "Cubs", "Brewers", 30);
            CheckRivalry(game, "Cubs", "Cardinals", 50);
            CheckRivalry(game, "Mets", "Braves", 15);
            CheckRivalry(game, "Mets", "Yankees", 20);
            CheckRivalry(game, "White Sox", "Cubs", 35);
            CheckRivalry(game, "Yankees", "Dodgers", 30);
            CheckRivalry(game, "Red Sox", "Rays", 30);
        }

        /// <summary>
        /// Update the game ratio
        /// </summary>
        private void UpdateRatio(Game game, int score, string desc)
        {
            game.Ratio += score;
            string scoreDesc = "+" + score;
            if (score < 0)
            {
                scoreDesc = "-" + score;
            }
        }

        /// <summary>
        /// Check rivalties
        /// </summary>
        private void CheckRivalry(Game game, string teamA, string teamB, int score)
        {
            if (game.AwayTeam.Name.Equals(teamA) || game.HomeTeam.Name.Equals(teamA))
            {
                if (game.AwayTeam.Name.Equals(teamB) || game.HomeTeam.Name.Equals(teamB))
                {
                    UpdateRatio(game, score, "Rivalry between "+teamA+" and "+teamB);
                }
            }
        }
    }
}
